import {Button, Col, Form, Row, TimePicker, Toast} from "@douyinfe/semi-ui";
import {Tooltip} from "@mui/material";
import {IconHelpCircle} from "@douyinfe/semi-icons";
import {chtimer, getcardlist} from "../../api/api.js";

import React, { useEffect, useRef, useImperativeHandle, forwardRef } from "react";

const Cmodal_form = forwardRef((props, ref) => {
    const childRef = useRef();
    useEffect(() => {
        console.log(props.info);
        api.current.setValue('name', props.info.methodName);
        api.current.setValue('timer', props.info.cron);
    }, [props.info]);

    const api = useRef();
    function vali(fm) {
        api.current.validate()
            .then(values=>{
                chtimer(fm).then(function (res) {
                    if (res.data.code==200){
                        Toast.success('任务修改成功')
                    }else {
                        Toast.error('任务修改失败')
                    }
                })
            })
            .catch(errors=>{ Toast.error('请检查表单后重新提交')})
    }


    useImperativeHandle(ref, () => ({
        kaishi: () => {
            console.log(api.current.getValues())
            const date = new Date(api.current.getValue("timer"));
            const localHours = date.getHours();
            const localMinutes = date.getMinutes();
            const localSeconds = date.getSeconds();
            const localTime = `${localHours.toString().padStart(2, '0')}:${localMinutes.toString().padStart(2, '0')}:${localSeconds.toString().padStart(2, '0')}`;

            const form = new FormData();
            form.append("id",props.info.id)
            form.append("state",localTime)
            vali(form)
        }
    }));

    return (
        <Form getFormApi={formApi => api.current = formApi} layout='horizontal'>
            <Row style={{ marginBottom: "20px" }}>
                <Col span={12}><Form.Input disabled={true} field='name' label='方法名' style={{ width: 220 }} /></Col>
                <Col span={12}><Form.Input disabled={true} field='dss' label='参数' style={{ width: 220 }} /></Col>
            </Row>
            <Row><Form.TimePicker
                rules={[
                    { required: true, message: '' },
                    // { type: 'string', message: 'type error' },
                    { validator: (rule, value) => value != null, message: '不能为空' },
                ]}
                label={'开启时间'} format={'HH:mm:ss'} field='timer' style={{ width: 220 }} scrollItemProps={{ mode: "wheel", cycled: true }} />
            </Row>
        </Form>
    );
});

export default Cmodal_form;
